package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.AdChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    /**
     * 查询频道列表带分页
     * @param dto 查询条件
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(AdChannelDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 检查分页参数
        dto.checkParam();

        //2 执行查询
        // 构建分页对象
        IPage<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());

        // 构建条件查询对象
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        // 频道模糊查询
        if (StringUtils.isNotBlank(dto.getName())) {
            // sql: name like '%java%'
            wrapper.like(AdChannel::getName, dto.getName());
        }
        if (dto.getStatus() != null) {
            wrapper.eq(AdChannel::getStatus, dto.getStatus());
        }

        // 按照ord 排序 升序
        wrapper.orderByAsc(AdChannel::getOrd);

        IPage<AdChannel> pageResult = page(page, wrapper);

//        int i = 1 / 0;
//
//        if (true) {
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }

        //3 封装返回结果
        PageResponseResult result =
                new PageResponseResult(dto.getPage(), dto.getSize(),
                        (int)pageResult.getTotal(), pageResult.getRecords() );
        return result;
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1 参数检查
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行新增
        adChannel.setCreatedTime(new Date());
        adChannel.setIsDefault(false); // 非默认， 有且只有一个频道是默认的
        save(adChannel);

        //3 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道&频道状态
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1 参数检查
        if (adChannel == null || adChannel.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 先查后改
        AdChannel channel = getById(adChannel.getId());
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //2 执行修改
        updateById(adChannel);

        //3 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //1 参数检查
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2 先查询判断状态，
        AdChannel channel = getById(id);
        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 启用状态下不能删除
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_CHANNEL_ISSTATUS_ERROR);
        }
        // TODO 判断当前判断是否被引用

        // 执行删除
        removeById(id);

        //3 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
